use crate::id;
use chrono::NaiveDate;
use sea_orm::prelude::async_trait::async_trait;
use sea_orm::{ActiveModelBehavior, DeriveEntityModel, DeriveRelation, EnumIter};
use sea_orm::{ActiveValue, PrimaryKeyTrait};
use sea_orm::{ConnectionTrait, DbErr, DerivePrimaryKey};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(schema_name = "sea", table_name = "tb_stop")]
#[serde(rename_all = "camelCase")]
pub struct Model {
    #[sea_orm(primary_key, auto_increment = false)]
    pub stop_id: i64,
    #[sea_orm(primary_key, auto_increment = false)]
    pub version_date: NaiveDate,
    pub expiration_date: NaiveDate,
    pub stop_name: String,
    pub direction: String,
    pub deleted: bool,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

#[async_trait]
impl ActiveModelBehavior for ActiveModel {
    async fn before_save<C>(mut self, _: &C, insert: bool) -> Result<Self, DbErr>
    where
        C: ConnectionTrait,
    {
        if insert {
            self.stop_id = ActiveValue::Set(id::next_id());
        }
        Ok(self)
    }
}